﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace HttpService
{
    /// <summary>
    /// http request
    /// </summary>
    public class Request
    {
        public Dictionary<string, string> Headers = new Dictionary<string, string>();// request header
        public string RequestUrl;// request url
        public string UserHost;// user host
        public List<string> Paths = new List<string>();// url path split with '/'
        public String ClientHost;// client host
        public DateTime RequestTime = DateTime.Now;// request time
        //Stream InStream;
        byte[] buff = new byte[1000];
        int len;
        int pos;
        int datapos;
        Socket Socket;

        String ReadLine()
        {
            while (true)
            {
                while (len > pos)
                {
                    if (buff[pos] == '\n')
                    {
                        String line;
                        if (buff[pos - 1] == '\r')// 越过\r
                            line = Encoding.Default.GetString(buff, datapos, pos - datapos - 1);
                        else
                            line = Encoding.Default.GetString(buff, datapos, pos - datapos);
                        pos++;
                        datapos = pos;
                        return line;
                    }
                    pos++;
                }
                if (datapos > 0)
                {
                    Array.Copy(buff, 0, buff, datapos, len - datapos);
                    len = pos = len - datapos;
                }

                if (buff.Length - len <= 0)
                    throw new IOException();

                int readed = Socket.Receive(buff, len, buff.Length - len, SocketFlags.None);
                if (readed <= 0)
                {
                    throw new SocketException(10049);
                }
                len += readed;
            }
        }

        public Request(Socket client)
        {
            try
            {
                UserHost = (client.RemoteEndPoint as IPEndPoint).Address.ToString();
            }
            catch
            { }
            //InStream = stream;
            Socket = client;

            String line = ReadLine();
            String[] args = line.Split(' ');
            RequestUrl = HttpUtility.UrlDecode(args[1]);
            int i = 0;
            foreach (var path in args[1].Split('/'))
            {
                if (i > 0) Paths.Add(HttpUtility.UrlDecode(path));
                i++;
            }

            line = ReadLine();
            while (line != null)
            {
                if (line.Length == 0)
                    break;

                int pos = line.IndexOf(':');
                if (pos == -1)
                    Headers.Add(line, "");
                else
                    Headers.Add(line.Substring(0, pos), line.Substring(pos + 1));

                line = ReadLine();
            }
        }
    }
}
